package top.lcm889.oj.offer.按数据结构分类.链表;

import java.util.*;

/**
 * @author Coder-lcm
 * @date 2021/2/5
 */
public class T6_从尾到头打印链表 {
    public static int[] reversePrint(ListNode head) {
        //栈：先进后出
        Stack<Integer> valStack = new Stack<>();
        if (head == null) return new int[0];
        while (head != null){
            valStack.push(head.val);
            head = head.next;
        }
        int len = valStack.size();
        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            res[i] = valStack.pop();
        }
        return res;
    }

    public static void main(String[] args) {
        int[] res = reversePrint(new ListNode(1, new ListNode(3, new ListNode(2))));
        for (int i : res) {
            System.out.println(i);
        }

    }
}
